﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using StoveTop.Models;

/* Display Recipe Information */

namespace StoveTop.Screens
{
	[Activity (Label = "StoveTopRecipeScreen")]			
	public class StoveTopRecipeScreen : Activity
	{

		protected override void OnCreate (Bundle bundle)
		{

			Log.Debug ("StoveTopRecipeScreen.OnCreate()", "Starting");
			base.OnCreate (bundle);

			int recipeId = Intent.GetIntExtra ("recipeId", -1);

			if (recipeId >= 0) {  // Load recipe from DB
				StoveTopDBHelper db = new StoveTopDBHelper (this);
				StoveTop.Models.Recipe recipeRow = db.GetRecipe (recipeId);
				if (recipeRow == null) {
					Console.WriteLine ("No recipe with recipeId " + recipeId.ToString () + " found");
					return;
				} else {
					Console.WriteLine ("About to create step list");
					List <Step> steps = db.GetAllStepsForRecipe (recipeId);
					int[] stepIds = new int[steps.Count];
					for (int i = 0; i < steps.Count; i++) {
						stepIds [i] = steps[i].GetId ();
					}


					List <StoveTop.Models.RecipeIngredient> ingredRows = db.GetAllIngredientsForRecipe(recipeId);
					Console.WriteLine (ingredRows.Count ().ToString () + " ingreds found");
					foreach (StoveTop.Models.RecipeIngredient ingredRow in ingredRows) {
						Console.WriteLine (ingredRow.ToString ());
					}
					List <RecipeIngredient> ingreds = new List <RecipeIngredient>();
					foreach (StoveTop.Models.RecipeIngredient ingredRow in ingredRows) {
						Log.Debug ("RecipeScreen", "Looking up name of ingredient id=" + ingredRow.GetIngredientId ().ToString ());
						string name = db.GetIngredient(ingredRow.GetIngredientId()).GetName();
						RecipeIngredient ingred = new RecipeIngredient (name, ingredRow.GetQuantityRequired (), ingredRow.GetUnits (), ingredRow.GetPrepNotes ());
						ingreds.Add (ingred);
					}
					List <Vocab> vocab = new List <Vocab> ();
					List <Tag> tags = db.GetAllTagsForRecipe (recipeId);
					Recipe recipe = new Recipe (recipeRow.GetName (), recipeRow.GetPrepTime (), recipeRow.GetCookTime (), recipeRow.GetRating (), recipeRow.GetDifficulty (), steps, ingreds, vocab, tags);

					SetContentView (Resource.Layout.RecipeScreenLayout);

					TextView recipeName = FindViewById<TextView> (Resource.Id.recipeName);
					TextView cookTime = FindViewById<TextView> (Resource.Id.cookTime);
					TextView prepTime = FindViewById<TextView> (Resource.Id.prepTime);
					TextView rating = FindViewById<TextView> (Resource.Id.rating);
					TextView difficulty = FindViewById<TextView> (Resource.Id.difficulty);
					TextView ingredsView= FindViewById<TextView> (Resource.Id.ingredients);
					TextView vocabView = FindViewById<TextView> (Resource.Id.vocab);
					recipeName.SetText (recipe.GetName (), TextView.BufferType.Normal);

					// Compute cook and prep times in min:s format
					int cookTimeValue = recipe.GetCookTime ();
					int cookTimeSecs = cookTimeValue % 60;
					int cookTimeMins = (cookTimeValue - cookTimeSecs) / 60;
					int prepTimeValue = recipe.GetPrepTime ();
					int prepTimeSecs = prepTimeValue % 60;
					int prepTimeMins = (prepTimeValue - prepTimeSecs) / 60;

					cookTime.SetText ("Cook Time: " + cookTimeMins.ToString() + "mins " + cookTimeSecs.ToString() + "secs", TextView.BufferType.Normal);
					prepTime.SetText ("Prep Time: " + prepTimeMins.ToString() + "mins " + prepTimeSecs.ToString() + "secs", TextView.BufferType.Normal);
					rating.SetText ("Rating: " + recipe.GetRating ().ToString () + " stars", TextView.BufferType.Normal);
					difficulty.SetText ("Difficulty: " + recipe.GetDifficulty ().ToString () + " stars", TextView.BufferType.Normal);
					string ingredientsText = "INGREDIENTS:\n";
					foreach (RecipeIngredient ingred in recipe.GetIngreds()) {
						ingredientsText += ingred.GetName () + ": ";
						ingredientsText += ingred.GetQuantityRequired ().ToString () + " " + ingred.GetUnits () + "\t" + ingred.GetPrepNotes() + "\n";
					}
					string vocabText = "VOCAB:\n";
					foreach (Vocab vc in recipe.GetVocab()) {
						vocabText += vc.GetName () + "\n";
					}
					ingredsView.SetText (ingredientsText, TextView.BufferType.Normal);
					vocabView.SetText (vocabText, TextView.BufferType.Normal);

					Button shoppingListButton = FindViewById<Button> (Resource.Id.shoppingListButton);
					shoppingListButton.Click += (sender, e) => {
						foreach (StoveTop.Models.RecipeIngredient ingred in ingredRows) {
							db.AddToShoppingList(ingred.GetIngredientId(), ingred.GetQuantityRequired());
							Android.Widget.Toast.MakeText(this, "Added ingredients to shopping list!", Android.Widget.ToastLength.Short).Show();
						}
					};
					Button start = FindViewById<Button> (Resource.Id.start);
					start.Click += (sender, e) => {
						var stepScreen = new Intent(this, typeof(StepScreen));
						stepScreen.PutExtra("stepIds", stepIds);
						stepScreen.PutExtra("stepNum", 0);
						StartActivity(stepScreen);
					};

				}
			} else {  // No recipeId passed
				Console.WriteLine ("No recipeId passed to RecipeScreen Activity");
				return;
			}
		}
	}
}